package com.chain.BrotherExchange.viewmodel;

import android.app.Application;
import android.arch.lifecycle.AndroidViewModel;
import android.arch.lifecycle.MutableLiveData;
import android.support.annotation.NonNull;

import com.chain.BrotherExchange.data.DataRepository;
import com.chain.BrotherExchange.data.Resource;
import com.chain.BrotherExchange.data.model.AddLegalTenderOrder;
import com.chain.BrotherExchange.utils.Androids;
import com.chain.BrotherExchange.utils.Strings;

import java.text.DecimalFormat;

/**
 * 法币下单
 */
public class LegalTenderAddOrderViewModel extends AndroidViewModel {

    private int uid = Androids.LocalConfig.getUid();
    private String token = Androids.LocalConfig.getToken();

    // 单价
    private double mUnitPrice;

    private MutableLiveData<Resource<AddLegalTenderOrder.DataBean>> mAddOrderLiveData
            = new MutableLiveData<>();

    private MutableLiveData<String> mTotalPrice = new MutableLiveData<>();
    private MutableLiveData<String> mTransactionNumber = new MutableLiveData<>();

    public LegalTenderAddOrderViewModel(@NonNull Application application) {
        super(application);
    }

    public MutableLiveData<String> getTotalPrice() {
        return mTotalPrice;
    }

    public MutableLiveData<String> getTransactionNumber() {
        return mTransactionNumber;
    }

    // 添加订单
    public MutableLiveData<Resource<AddLegalTenderOrder.DataBean>> addOrder(int orderId, String number) {

        if (Strings.isEmpty(number)) {
            mAddOrderLiveData.setValue(Resource.error("请输入购买数量"));
            return mAddOrderLiveData;
        }

        double numberDouble = Double.parseDouble(number);
        mAddOrderLiveData.setValue(Resource.loading());
        DataRepository.get().legalTenderAddOrder(uid, token, orderId, numberDouble, result -> {
            if (result.isSucceed()) {
                mAddOrderLiveData.setValue(Resource.success(result.getData(), result.getMsg()));
            } else {
                mAddOrderLiveData.setValue(Resource.error(result.getMsg()));
            }
        });

        return mAddOrderLiveData;
    }

    public void setUnitPrice(double unitPrice) {
        this.mUnitPrice = unitPrice;
    }

    private String number;

    public void setTotalPrice(String totalPriceString) {
//        if (totalPriceString.length() > 0) {
//            double totalPrice = Double.parseDouble(totalPriceString);
//            String number = new DecimalFormat("#########.#########")
//                    .format(totalPrice / mUnitPrice);
//            if (!number.equals(this.number)) {
//                this.totalPrice = totalPriceString;
//                mTransactionNumber.setValue(number);
//            }
//        } else {
//            mTransactionNumber.setValue(null);
//        }
    }

    private String totalPrice;

    public void setTransactionNumber(String numberString) {
        if (numberString.length() > 0) {
            double number = Double.parseDouble(numberString);
            String totalPrice = new DecimalFormat("#########.#########")
                    .format(number * mUnitPrice);

            if (!totalPrice.equals(this.totalPrice)) {
                this.number = numberString;
                mTotalPrice.setValue(totalPrice);
            }
        } else {
            mTotalPrice.setValue(null);
        }
    }
}
